package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.dto.ElderPageQueryDto;
import com.zzyl.nursing.domain.vo.PageElderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.domain.pojo.Elder;
import com.zzyl.nursing.service.IElderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 老人Service业务层处理
 * 
 * @author alexis
 * @date 2025-05-19
 */
@Service
public class ElderServiceImpl extends ServiceImpl<ElderMapper, Elder> implements IElderService
{
    @Autowired
    private ElderMapper elderMapper;

    /**
     * 查询老人
     * 
     * @param id 老人主键
     * @return 老人
     */
    @Override
    public Elder selectElderById(Long id)
    {
        return elderMapper.selectById(id);
    }

    /**
     * 查询老人列表
     * 
     * @param elder 老人
     * @return 老人
     */
    @Override
    public List<Elder> selectElderList(Elder elder)
    {
        return elderMapper.selectElderList(elder);
    }

    /**
     * 新增老人
     * 
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int insertElder(Elder elder)
    {
        return elderMapper.insert(elder);
    }

    /**
     * 修改老人
     * 
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int updateElder(Elder elder)
    {
        return elderMapper.updateById(elder);
    }

    /**
     * 批量删除老人
     * 
     * @param ids 需要删除的老人主键
     * @return 结果
     */
    @Override
    public int deleteElderByIds(Long[] ids)
    {
        return elderMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除老人信息
     * 
     * @param id 老人主键
     * @return 结果
     */
    @Override
    public int deleteElderById(Long id)
    {
        return elderMapper.deleteById(id);
    }

    /**
     * 分页查询已入住的老人列表
     * @param elderPageQueryDto
     * @return
     */
    @Override
    public TableDataInfo<PageElderVo> pageQuery(ElderPageQueryDto elderPageQueryDto) {
        // 分页查询
        String name = elderPageQueryDto.getName();
        String idCardNo = elderPageQueryDto.getIdCardNo();
        // 查询条件
        // 方法1
//        this.page(page, Wrappers.<Elder>lambdaQuery()
//                .eq(ObjectUtils.isNotNull(elderPageQueryDto.getStatus()),Elder::getStatus, elderPageQueryDto.getStatus())
//                .like(ObjectUtils.isNotNull(name),Elder::getName,name)
//                .like(ObjectUtils.isNotNull(idCardNo),Elder::getIdCardNo,idCardNo)
//                .orderByDesc(Elder::getCreateTime)
//        );
        // 写法二
        Page<Elder> page = this.lambdaQuery()
                .eq(ObjectUtils.isNotNull(elderPageQueryDto.getStatus()), Elder::getStatus, elderPageQueryDto.getStatus())
                .like(ObjectUtils.isNotNull(name), Elder::getName, name)
                .like(ObjectUtils.isNotNull(idCardNo), Elder::getIdCardNo, idCardNo)
                .orderByDesc(Elder::getCreateTime)
                .page(Page.of(elderPageQueryDto.getPageNum(), elderPageQueryDto.getPageSize()));
        // 转换为PageElderVo列表
        List<PageElderVo> pageElderVos = BeanUtil.copyToList(page.getRecords(), PageElderVo.class);
        return TableDataInfo.getDataTable(pageElderVos, page.getTotal());
    }
}
